package com.tk.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tk.util.*;
import com.tk.entity.Post;

public class DisplayTheme extends HttpServlet {

	public DisplayTheme() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request,response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//request.setCharacterEncoding("UTF-8");
		
		Connection conn = DBUtil.getConnection();
		ResultSet rs = null;
		String sql = "select * from post where pid = 0";
		List<Post> list = new ArrayList<Post>();
		
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				Post post = new Post();
				post.initPostByResultSet(rs);
				list.add(post);
			}
			DBUtil.closeAll(ps, rs, conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		request.setAttribute("posts", list);
		request.getRequestDispatcher("../home/forum.jsp").forward(request, response);
		
	}

}
